package com.dog.basics.common.util;

import cn.hutool.core.date.DateUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
import java.util.Date;

/**
 * @description: 编号工具类
 * @author: He
 * @date: Created in 2020/7/30 11:11
 * @version: 1.0
 * @modified By:
 */
public class IdentifierUtil {

    /**
     * 生成规则设备编号:设备类型+五位编号（从1开始，不够前补0）
     * @param equipmentType
     *              设备类型
     * @param equipmentNo
     *              最新设备编号
     * @return
     */
    public static String getNewEquipmentNo(String equipmentType, String equipmentNo){
        String newEquipmentNo =equipmentType+"00001";
        if(equipmentNo != null && !equipmentNo.isEmpty()){
            int newEquipment = Integer.parseInt(equipmentNo) + 1;
            newEquipmentNo = String.format(equipmentType + "%05d", newEquipment);
        }
        return newEquipmentNo;
    }

    /**
     * 得到中文首字母（清华大学 -> QHDX）
     * @param str 需要转化的中文字符串
     * @return 大写首字母缩写的字符串
     */
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString().toUpperCase();
    }

    public static void main(String args[]){
        String yyMM = DateUtil.format(new Date(), "yyMM");
        System.out.println(getNewEquipmentNo(yyMM,"99999").substring(4,9));
        String head = IdentifierUtil.getPinYinHeadChar("清华");
        if (head.length()>3){
            head=head.substring(0,3);
        }
        String code1="12312300001";
        String  code = IdentifierUtil.getNewEquipmentNo(head,code1.substring(code1.length()-5));
        System.out.println(code);
    }

}
